/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package demo_login;

import dao.Student;
import db.JDBCUtil;
import gui.MainFrame;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author C079
 */
public class NewJFrame_find extends javax.swing.JFrame {
    List<Student> ll;

    public List<Student> getLl() {
        return ll;
    }

    public void setLl(List<Student> ll) {
        this.ll = ll;
    }
    
    /**
     * Creates new form NewJFrame_find
     */
    public NewJFrame_find() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jButton_findsave = new javax.swing.JButton();
        jButton_findcancle = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();

        jLabel1.setText("方法一：请输入学号");

        jLabel2.setText("方法二：请输入姓名");

        jButton_findsave.setText("确认");
        jButton_findsave.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButton_findsaveMousePressed(evt);
            }
        });
        jButton_findsave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_findsaveActionPerformed(evt);
            }
        });

        jButton_findcancle.setText("取消");
        jButton_findcancle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_findcancleActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 303, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(jButton_findsave)
                .addGap(106, 106, 106)
                .addComponent(jButton_findcancle))
            .addGroup(layout.createSequentialGroup()
                .addGap(67, 67, 67)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(24, 24, 24)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_findsave)
                    .addComponent(jButton_findcancle))
                .addGap(45, 45, 45))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_findcancleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_findcancleActionPerformed
        this.setVisible(false);
        new MainFrame().setVisible(true);
    }//GEN-LAST:event_jButton_findcancleActionPerformed

    private void jButton_findsaveMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton_findsaveMousePressed
        // TODO add your handling code here:
       if(!jTextField1.getText().trim().equals("")&&!jTextField2.getText().trim().equals("")){  try {
            // TODO add your handling code here:
            
            String id=jTextField1.getText().trim();
            String name=jTextField2.getText().trim();
            Connection conn=JDBCUtil.connectiontt();
            String sql="select * from students where id=?";
            PreparedStatement pstat = conn.prepareStatement(sql);
            pstat.setString(1, id);
            ResultSet rs=pstat.executeQuery();
            if(rs.next()){
             String sql1="select * from students where id=? and name=?";
            PreparedStatement pstat1 = conn.prepareStatement(sql1);
            pstat1.setString(1, id);
            pstat1.setString(2, name);
            ResultSet rs1=pstat1.executeQuery();
             if(rs1.next()){
              JOptionPane.showMessageDialog(null, "有此学生");
             }else{
             JOptionPane.showMessageDialog(null, "姓名错误");
             }
            }else{
                JOptionPane.showMessageDialog(null, "id错误");
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "运行错误");
        }
       }
       if(!jTextField1.getText().trim().equals("")&&jTextField2.getText().trim().equals("")){
           try {
               String id=jTextField1.getText().trim();
               Connection conn=JDBCUtil.connectiontt();
               String sql="select * from students where id=?";
               PreparedStatement pstat = conn.prepareStatement(sql);
               pstat.setString(1, id);
               ResultSet rs=pstat.executeQuery();
               if(rs.next()){JOptionPane.showMessageDialog(null, "有此学生");
               }
               else{
                JOptionPane.showMessageDialog(null, "无此学生");
               }
           } catch (SQLException ex) {
               Logger.getLogger(NewJFrame_find.class.getName()).log(Level.SEVERE, null, ex);
           }
        
    }//GEN-LAST:event_jButton_findsaveMousePressed
       if(!jTextField2.getText().trim().equals("")&&jTextField1.getText().trim().equals("")){
           try {
               String id=jTextField1.getText().trim();
               Connection conn=JDBCUtil.connectiontt();
               String sql="select * from students where id=?";
               PreparedStatement pstat = conn.prepareStatement(sql);
               pstat.setString(1, id);
               ResultSet rs=pstat.executeQuery();
               if(rs.next()){JOptionPane.showMessageDialog(null, "有此学生");
               }
               else{
                JOptionPane.showMessageDialog(null, "无此学生");
               }
           } catch (SQLException ex) {
               Logger.getLogger(NewJFrame_find.class.getName()).log(Level.SEVERE, null, ex);
           }
        
    }
    }
    
    private void jButton_findsaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_findsaveActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton_findsaveActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame_find.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame_find.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame_find.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame_find.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
            	NewJFrame_find find= new NewJFrame_find();
            	find.setVisible(true);
            	find.setTitle("学生信息查找界面");
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_findcancle;
    private javax.swing.JButton jButton_findsave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
